<template>
  <div>
    <div class="rightTop">
      <el-button @click="Return">返回</el-button>
    </div>
    <div class="mainList">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="300px"
        class="demo-ruleForm"
        style="margin-top: 30px;width:100%"
      >
        <el-form-item label="活动标题：" prop="title" style="width:900px">
          <el-input v-model="ruleForm.title" required>{{ruleForm.title}}</el-input>
        </el-form-item>
        <el-form-item label="开始时间：">
          <el-col :span="11" required>
            <el-form-item prop="startTime">
              <el-date-picker
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期"
                v-model="ruleForm.startTime"
                style="width: 100%"
              >{{ruleForm.startTime}}</el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="结束时间：">
          <el-col :span="11" required>
            <el-form-item prop="endTime">
              <el-date-picker
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期"
                v-model="ruleForm.endTime"
                style="width: 100%;"
                @change="endTimes"
              >{{ruleForm.endTime}}</el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="上架/下架">
          <el-radio v-model="ruleForm.putType" :label="1">上线</el-radio>
          <el-radio v-model="ruleForm.putType" :label="0">下架</el-radio>
        </el-form-item>

        <el-form-item label="优惠条件：" style="width:900px">
          <el-table :data="ruleForm.DiscountData" tooltip-effect="dark">
            <el-table-column label="层级" prop="level">
              <template slot-scope="scope">{{scope.row.level}}</template>
            </el-table-column>
            <el-table-column label="优惠门槛" show-overflow-tooltip>
              <template slot-scope="scope">
                <font slot="font-style: normal">满：</font>
                <el-input
                  label="threshold"
                  style="width: 90px; text-align: center; font-style: normal"
                  v-model="scope.row.threshold"
                >
                  <i slot="suffix">元</i>
                </el-input>
              </template>
            </el-table-column>
            <el-table-column label="优惠方式" show-overflow-tooltip>
              <template slot-scope="scope">
                <font slot="font-style: normal">减：</font>
                <el-input
                  label="mode"
                  style="width: 90px;text-align: center; font-style: normal"
                  v-model="scope.row.mode"
                >
                  <i slot="suffix">元</i>
                </el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="deleteOne(scope.$index, scope.row)"
                  type="text"
                  :disabled="scope.$index==ruleForm.DiscountData.length-1?false:true"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-form-item>
            <el-button @click="addColumn()" class="addColumn">新增优惠规则</el-button>最多可添加五个层级
          </el-form-item>
        </el-form-item>
        <el-form-item label="参与活动商品：" prop>
          <el-radio-group v-model="ruleForm.category" @change="agreeChange">
            <el-radio :label="'alls'">全部商品</el-radio>
            <el-radio :label="'type'">指定分类</el-radio>
            <el-radio :label="'goods'">指定商品</el-radio>
            <el-radio :label="'brands'">指定品牌</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <div class="tabZujian">
            <el-tabs v-model="activeGood">
              <el-tab-pane label="全部商品" name="alls" :key="'alls'" disabled>
                <el-tabs type="border-card">
                  <span>满减活动选择全部商品后，在活动保存成功后，店铺再添加的商品是不参与到满减活动中的</span>
                </el-tabs>
              </el-tab-pane>

              <el-tab-pane label="指定分类" name="type" :key="'type'" disabled>
                <el-tabs type="border-card">
                  <el-tab-pane label="全部分类">
                    <el-tree
                      :data="allClass"
                      show-checkbox
                      node-key="id"
                      ref="tree"
                      default-expand-all
                      :default-checked-keys="myPermiss"
                      highlight-current
                      :props="defaultProps"
                      @check-change="handleCheckChange"
                    ></el-tree>
                  </el-tab-pane>
                  <el-tab-pane label="已选分类">
                    <el-tree
                      :data="selectClass"
                      :props="defaultProps"
                      ref="wtree"
                      node-key="id"
                      default-expand-all
                      show-checkbox
                      :default-checked-keys="myPermass"
                      @check-change="handleNodeClick"
                    ></el-tree>
                  </el-tab-pane>
                </el-tabs>
              </el-tab-pane>

              <el-tab-pane label="指定商品" name="goods" :key="'goods'" :disabled="madd">
                <el-tabs type="border-card">
                  <el-tab-pane label="全部商品" :data="allGoods">
                    <el-header style="height: auto">
                      <span style="width:900px">
                        <el-form-item>
                          <el-select
                            placeholder="按分类选择"
                            v-model="allGoods.sortId"
                            style="width:100px"
                          >
                            <el-option label="全部" value></el-option>
                            <el-option label="球/皮鞋类型" value="1"></el-option>
                            <el-option label="手机/数码" value="7"></el-option>
                            <el-option label="服装/服饰" value="11"></el-option>
                          </el-select>
                          <el-select
                            placeholder="按品牌选择:"
                            v-model="allGoods.brandId"
                            style="width:100px"
                          >
                            <el-option label="全部" value></el-option>
                            <el-option label="华为" value="1"></el-option>
                            <el-option label="小米" value="2"></el-option>
                            <el-option label="苹果" value="3"></el-option>
                            <el-option label="耐克" value="4"></el-option>
                            <el-option label="杰克.琼斯" value="5"></el-option>
                          </el-select>
                          <el-input
                            placeholder="商品名称/条形码"
                            v-model="allGoods.title"
                            style="width: 100px"
                          ></el-input>
                          <el-button
                            @click="Search()"
                            style="background-color: skyblue ; color: white"
                          >搜索商品</el-button>
                          <el-button @click="pageSelect">本页全选</el-button>
                          <el-button @click="pageQuXiao">本页取消</el-button>
                        </el-form-item>
                      </span>
                    </el-header>
                    <el-main style="height: 600px">
                      <div>
                        <el-row :gutter="5">
                          <el-col
                            :span="6"
                            style="width: 200px; margin-right: 25px; height:280px"
                            v-for="goodsData in allGoods.goodsData"
                            :key="goodsData.value"
                          >
                            <el-image
                              style="width: 100px; height: 100px; text-align: center"
                              :src="goodsData.picture"
                            ></el-image>
                            <div>{{ goodsData.title }}</div>
                            <span>
                              <div>￥{{ goodsData.price }}</div>
                              <el-button
                                class="addColumn"
                                ref="bin"
                                @click="SelectInt(goodsData.id,goodsData)"
                              >{{ goodsData.status ==0?"未选择":"已选择" }}</el-button>
                            </span>
                          </el-col>
                        </el-row>
                      </div>
                    </el-main>
                    <el-footer style="height: auto">
                      <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage4"
                        :page-sizes="pageSizes"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                      ></el-pagination>
                    </el-footer>
                  </el-tab-pane>

                  <el-tab-pane label="已选商品" :data="selectGood">
                    <el-header style="height: auto">
                      <span style="width:900px">
                        <el-form-item>
                          <el-select
                            placeholder="按分类选择"
                            v-model="selectGood.sortId"
                            style="width:100px"
                          >
                            <el-option label="全部" value></el-option>
                            <el-option label="球/皮鞋类型" value="1"></el-option>
                            <el-option label="手机/数码" value="7"></el-option>
                            <el-option label="服装/服饰" value="11"></el-option>
                          </el-select>
                          <el-select
                            placeholder="按品牌选择:"
                            v-model="selectGood.brandId"
                            style="width:100px"
                          >
                            <el-option label="全部" value></el-option>
                            <el-option label="华为" value="1"></el-option>
                            <el-option label="小米" value="2"></el-option>
                            <el-option label="苹果" value="3"></el-option>
                            <el-option label="耐克" value="4"></el-option>
                            <el-option label="杰克.琼斯" value="5"></el-option>
                          </el-select>
                          <el-input
                            placeholder="商品名称/条形码"
                            v-model="selectGood.title"
                            style="width: 110px"
                          ></el-input>
                          <el-button
                            @click="SearchA()"
                            style="background-color: skyblue ; color: white"
                          >搜索商品</el-button>
                          <el-button @click="pageQuXiaoA">本页取消</el-button>
                        </el-form-item>
                      </span>
                    </el-header>
                    <el-main style="height: 600px">
                      <el-row :gutter="10">
                        <el-col
                          :span="6"
                          style="width: 200px; margin-right: 20px; height:280px"
                          v-for="goodsDataA in selectGood.goodsDataA"
                          :key="goodsDataA.value"
                        >
                          <el-image style="width: 100px; height: 100px;" :src="goodsDataA.picture"></el-image>
                          <p>{{ goodsDataA.title }}</p>
                          <span>
                            <div>￥{{ goodsDataA.price }}</div>
                            <el-button
                              class="addColumn"
                              @click="SelectIntA(goodsDataA.id,goodsDataA)"
                            >{{ goodsDataA.status ==0?"未选择":"已选择"}}</el-button>
                          </span>
                        </el-col>
                      </el-row>
                    </el-main>
                    <el-footer>
                      <el-pagination
                        @size-change="handleSizeChange1"
                        @current-change="handleCurrentChange1"
                        :current-page="currentPage4"
                        :page-sizes="pageSizes"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total1"
                      ></el-pagination>
                    </el-footer>
                  </el-tab-pane>
                </el-tabs>
              </el-tab-pane>

              <el-tab-pane label="指定品牌" name="brands" :key="'brands'" :disabled="madd">
                <el-tabs type="border-card">
                  <el-tab-pane label="全部品牌" :data="allBank">
                    <el-header style="height: auto">
                      <span style="width:900px">
                        <el-form-item>
                          <el-select placeholder="按分类选择" v-model="allBank.sortId">
                            <el-option label="全部" value></el-option>
                            <el-option label="球/皮鞋类型" value="1"></el-option>
                            <el-option label="手机/数码" value="7"></el-option>
                            <el-option label="服装/服饰" value="11"></el-option>
                          </el-select>
                          <el-button
                            @click="SearchB()"
                            style="background-color: skyblue ; color: white"
                          >搜索商品</el-button>
                          <el-button @click="pageSelectB">本页全选</el-button>
                          <el-button @click="pageQuXiaoB">本页取消</el-button>
                        </el-form-item>
                      </span>
                    </el-header>
                    <el-main style="height: 600px">
                      <el-row :gutter="10">
                        <el-col
                          :span="6"
                          style="width: 200px; margin-right: 20px; height:280px"
                          v-for="brandData in allBank.brandData"
                          :key="brandData.value"
                        >
                          <el-image
                            style="width: 100px; height: 100px; text-align: center"
                            :src="brandData.picture"
                          ></el-image>
                          <p>{{ brandData.name }}</p>
                          <span>
                            <el-button
                              class="addColumn"
                              @click="SelectBrandA(brandData.id,brandData)"
                            >{{ brandData.state ==0?"未选择":"已选择"}}</el-button>
                          </span>
                        </el-col>
                      </el-row>
                    </el-main>
                    <el-footer style="height: auto">
                      <el-pagination
                        @size-change="handleSizeChange2"
                        @current-change="handleCurrentChange2"
                        :current-page="currentPage4"
                        :page-sizes="pageSizes"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total2"
                      ></el-pagination>
                    </el-footer>
                  </el-tab-pane>

                  <el-tab-pane label="已选品牌" :data="selectBank">
                    <el-header style="height: auto">
                      <span style="width:900px">
                        <el-form-item>
                          <el-select placeholder="按分类选择" v-model="selectBank.sortId">
                            <el-option label="全部" value></el-option>
                            <el-option label="球/皮鞋类型" value="1"></el-option>
                            <el-option label="手机/数码" value="7"></el-option>
                            <el-option label="服装/服饰" value="11"></el-option>
                          </el-select>
                          <el-button
                            @click="SearchC()"
                            style="background-color: skyblue ; color: white"
                          >搜索商品</el-button>
                          <el-button @click="pageQuXiaoC">本页取消</el-button>
                        </el-form-item>
                      </span>
                    </el-header>
                    <el-main style="height: 600px">
                      <el-row :gutter="10">
                        <el-col
                          :span="6"
                          style="width: 200px; margin-right: 20px; height:280px"
                          v-for="brandDataA in selectBank.brandDataA"
                          :key="brandDataA.value"
                        >
                          <el-image
                            style="width: 100px; height: 100px; text-align: center"
                            :src="brandDataA.picture"
                          ></el-image>
                          <p>{{ brandDataA.name }}</p>
                          <span>
                            <el-button
                              class="addColumn"
                              ref="btn"
                              @click="SelectBrandB(brandDataA.id,brandDataA)"
                            >{{ brandDataA.state ==0?"未选择":"已选择"}}</el-button>
                          </span>
                        </el-col>
                      </el-row>
                    </el-main>
                    <el-footer style="height: auto">
                      <el-pagination
                        @size-change="handleSizeChange3"
                        @current-change="handleCurrentChange3"
                        :current-page="currentPage4"
                        :page-sizes="pageSizes"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total3"
                      ></el-pagination>
                    </el-footer>
                  </el-tab-pane>
                </el-tabs>
              </el-tab-pane>
            </el-tabs>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">提交</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import rebateApi from "@/api/operate/rebateActivities";
import rebDiscountApi from "@/api/operate/rebDiscount";
import rebBrandApi from "@/api/operate/rebBrand";
import rebLinkApi from "@/api/operate/rebLink";
import rebGoodsApi from "@/api/operate/rebGoods";
import rebSortApi from "@/api/operate/rebSort";
import VBrandApi from "@/api/operate/VBrands";
import VGoodsApi from "@/api/operate/VGoods";
import VSortApi from "@/api/operate/VSort";
export default {
  data() {
    return {
      madd: true,

      validateDate:[],
      //开起筛选
      selectrue: true,

      myPermiss: [],
      myPermass: [],
      findData: [],
      nodeDate: [],
      //全选商品数据列
      selecData: [],
      idlistData:[],


      ids: this.$route.params.id,
      // 分页
      pageSize: 8,
      pageSizes: [8, 16, 24, 50, 100],
      total: 5,
      total1: 5,
      total2: 5,
      total3: 5,
      currentPage4: 1,

      activeGood: "alls",
      addOrUpdateId: "",
      ruleForm: {
        discountId:"",
        title: "",
        activityState:"",
        startTime: "",
        endTime: "",
        putType: "",
        category: "alls",
        DiscountData: []
      },
      rules: {
        title: [
          { required: true, message: "请输入活动标题", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        startTime: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur"
          }
        ],
        endTime: [
          {
            required: true,
            message: "不能为空",
            trigger:"blur"
          },
        ],
        DiscountData: [
          {
            type: "data",
            required: true,
            message: "不能超过五个层级",
            trigger: "change"
          }
        ]
      },
      //全部分类
      allClass: [],
      //已选分类
      selectClass: [],
      defaultProps: {
        children: "children",
        label: "label"
      },

      allGoods: {
        sortId: "",
        brandId: "",
        title: "",
        goodsData: []
      },
      selectGood: {
        sortId: "",
        brandId: "",
        title: "",
        goodsDataA: []
      },
      allBank: {
        sortId: "",
        brandData: []
      },
      selectBank: {
        sortId: "",
        brandDataA: []
      }
    };
  },



  mounted() {
    this.init(ids);

    var ids = parseInt(this.$route.params.id);

    this.indata(ids);

    this.indataA(this.currentPage4, this.pageSize, this.ids);

    this.indataB(this.currentPage4, this.pageSize, this.ids);

    this.indataC(this.currentPage4, this.pageSize, this.ids);

    this.indataD(this.currentPage4, this.pageSize, this.ids);

    this.getpermissData();

    this.getsortData(this.ids);

    this.getSortVDate(this.ids);

    this.getVSortData(this.ids);
  },

  methods: {

    endTimes(){
      if(this.ruleForm.endTime<this.ruleForm.startTime){
        this.$message({
          title: '警告',
          message: '结束时间大于开始时间',
          type: 'warning'
        });
      }
    },

    handleCheckChange() {
      let res = this.$refs.tree.getCheckedNodes();
      let arr = [];
      res.forEach(item => {
        arr.push(item.id);
      });
    },

    handleNodeClick() {
      let resa = this.$refs.wtree.getCheckedNodes();
      let arr = [];
      resa.forEach(item => {
        arr.push(item.id);
      });
    },

    //全部树状分类
    getpermissData() {
      rebSortApi.getFindSortList().then(Response => {
        const resp = Response.data;
        this.allClass = resp.data;
      });
    },

    //全部树状分类状态选中
    getsortData(ids) {
      rebLinkApi.findRebSortLink(this.ids).then(Response => {
        this.myPermiss = Response.data.data;
      });
    },

    //已选树状分类
    getVSortData(ids) {
      VSortApi.getVSortList(this.ids).then(Response => {
        this.myPermass = Response.data.data;
      });
    },

    //已选树状分类状态选中
    getSortVDate(ids) {
      VSortApi.getFindSorts(this.ids).then(Response => {
        const rsa = Response.data;
        this.selectClass = rsa.data;
      });
    },

    //满减详情查询
    indata(ids) {
      if(this.ids!=null){
      rebDiscountApi.getFindRebToDisList(this.ids).then(Response => {
        var data = JSON.parse(JSON.stringify(Response.data.data));
        this.ruleForm = data[0];
        this.ruleForm.discountId = data[0].discountId;
        this.ruleForm.title = data[0].title;
        this.ruleForm.activityState = data[0].activityState;
        this.ruleForm.startTime = data[0].startTime;
        this.ruleForm.endTime = data[0].endTime;
        this.ruleForm.putType = data[0].putType;
        this.ruleForm.category = data[0].category;
        this.ruleForm.DiscountData = data[0].list;
        if (this.ruleForm.category == "alls") {
          this.activeGood = "alls";
        }
        if (this.ruleForm.category == "type") {
          this.activeGood = "type";
        }
        if (this.ruleForm.category == "goods") {
          this.activeGood = "goods";
        }
        if (this.ruleForm.category == "brands") {
          this.activeGood = "brands";
        }
      });
      }else if(this.ids==null){
        this.$message.success({
              message: "新增满减活动",
            });
      }
    },

    //全部商品分页查询
    indataA(pageNum, pageSize, ids) {
      rebGoodsApi.getFindAllList(pageNum, pageSize, ids).then(Response => {
        this.allGoods.goodsData = Response.data.data.list;
        this.total = Response.data.data.total;
      });
    },

    //全部商品筛选
    indataAA(pageNum, pageSize, ids, sortId, brandId, title) {
      if (
        this.allGoods.sortId != null ||
        this.allGoods.brandId != null ||
        this.allGoods.title != null
      ) {
        this.selectrue = true;
      } else {
        this.selectrue = false;
      }
      rebGoodsApi
        .getScreenGoods(
          pageNum,
          pageSize,
          this.ids,
          this.allGoods.sortId,
          this.allGoods.brandId,
          this.allGoods.title
        )
        .then(Response => {
          this.allGoods.goodsData = Response.data.data.list;
          this.total = Response.data.data.total;
        });
    },

    //已选商品分页查询
    indataB(pageNum, pageSize, ids) {
      VGoodsApi.getFindGoods(pageNum, pageSize, ids).then(Response => {
        this.selectGood.goodsDataA = Response.data.data.list;
        this.selectGood.goodsDataA.forEach(re=>{
          if(re.status==0){
            re.status=1
          }
        })
        this.total1 = Response.data.data.total;
      });
    },

    //已选商品筛选
    indataBA(pageNum, pageSize, ids, sortId, brandId, title) {
      if (
        this.selectGood.sortId != null ||
        this.selectGood.brandId != null ||
        this.selectGood.title != null
      ) {
        this.selectrue = true;
      } else {
        this.selectrue = false;
      }
      VGoodsApi.getScreenVGoods(
        pageNum,
        pageSize,
        this.ids,
        this.selectGood.sortId,
        this.selectGood.brandId,
        this.selectGood.title
      ).then(Response => {
        this.selectGood.goodsDataA = Response.data.data.list;
        this.total1 = Response.data.data.total;
      });
    },

    //全部品牌分页查询
    indataC(pageNum, pageSize, ids) {
      rebBrandApi.findABrand(pageNum, pageSize, ids).then(Response => {
        this.allBank.brandData = Response.data.data.list;
        this.total2 = Response.data.data.total;
      });
    },

    //全部品牌筛选
    indataCA(pageNum, pageSize, ids, sortId) {
      if (this.allBank.sortId != null) {
        this.selectrue = true;
      } else {
        this.selectrue = false;
      }
      rebBrandApi
        .findScreenBrand(pageNum, pageSize, this.ids, this.allBank.sortId)
        .then(Response => {
          this.allBank.brandData = Response.data.data.list;
          this.total2 = Response.data.data.total;
        });
    },

    //已选品牌分页查询
    indataD(pageNum, pageSize, ids) {
      VBrandApi.findBrandSelect(pageNum, pageSize, ids).then(Response => {
        this.selectBank.brandDataA = Response.data.data.list;
        this.selectBank.brandDataA.forEach(rsk=>{
          if(rsk.state==0){
            rsk.state=1
          }
        })
        this.total3 = Response.data.data.total;
      });
    },

    //已选品牌筛选
    indataDA(pageNum, pageSize, ids, sortId) {
      if (this.selectBank.sortId != null) {
        this.selectrue = true;
      } else {
        this.selectrue = false;
      }
      VBrandApi.findBrandScreen(
        pageNum,
        pageSize,
        this.ids,
        this.selectBank.sortId
      ).then(Response => {
        this.selectBank.brandDataA = Response.data.data.list;
        this.total3 = Response.data.data.total;
      });
    },

    handleTreeFilter(value, data, node) {
      if (node.checked || node.indeterminate) {
        return true;
      } else {
        return false;
      }
    },

    init(ids) {
      this.addOrUpdateId = ids == null ? 0 : ids;
    },

    //提交修改
    submitForm() {
      if (this.ids != null) {
        var rebDiscount=JSON.stringify(this.ruleForm.DiscountData)
        var idList=JSON.stringify(this.idlistData)
        var dto = {"id":this.ids,"discountId":this.ruleForm.discountId,"title":this.ruleForm.title,
        "activityState":this.ruleForm.activityState,"startTime":this.ruleForm.startTime,
        "endTime":this.ruleForm.endTime,"putType":this.ruleForm.putType,"category":this.ruleForm.category};
        rebateApi.getUpdateRebateList(dto).then(Response=>{
          const reps=Response.data;
          if(reps.code==200){
            this.$router.replace({ path: "/operation/WithReducedActivity" });
          }else{
            this.$message({
              message: "失败",
              type: "warning"
            });
          }
        })
        rebDiscountApi.getFindDis(this.ids,rebDiscount,idList).then(Response=>{
          const repsm=Response.data;
          if(repsm.code==200){
            this.$router.replace({ path: "/operation/WithReducedActivity" });
          }else{
            this.$message({
              message: "检查失败",
              type: "warning"
            });
          }
        })
        if (dto.category == "type") {
          var dto={"rebateId":this.ids,"id":this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())}
          rebLinkApi.getUpdateLink(dto).then(Response=>{
            const uplink=Response.data
            if(uplink.code==200){
              this.$router.replace({ path: "/operation/WithReducedActivity" });
            }
          })
        }
        if (dto.category == "goods") {
          var vGoods=JSON.stringify(this.selectGood.goodsDataA)
          VGoodsApi.getGoodsUpdate(this.ids,vGoods).then(Response=>{
            const re=Response.data
            if(re.code==200){
              this.$router.replace({ path: "/operation/WithReducedActivity" });
            }
          })
        }
        if (dto.category == "brands") {
          var vBrands=JSON.stringify(this.selectBank.brandDataA)
          VBrandApi.getFindBraUpdate(this.ids,vBrands).then(Response=>{
            const vbra=Response.data
            if(vbra.code==200){
              this.$router.replace({ path: "/operation/WithReducedActivity" });
            }
          })
        }
      }
      if (this.ids == null) {
        var rebDiscounts=JSON.stringify(this.ruleForm.DiscountData)
        var dto = {"discountId":this.ruleForm.DiscountData.length,"title":this.ruleForm.title,
        "activityState":this.ruleForm.activityState,"startTime":this.ruleForm.startTime,
        "endTime":this.ruleForm.endTime,"putType":this.ruleForm.putType,"category":this.ruleForm.category};
        var rebate=JSON.stringify(dto)
        var reLink=JSON.stringify(this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()))
        var aGoods=JSON.stringify(this.selectGood.goodsDataA)
        var aBrands=JSON.stringify(this.selectBank.brandDataA)
        rebateApi.getSaveRebate(rebDiscounts,rebate,reLink,aGoods,aBrands).then(Response=>{
          const sva=Response.data
          if(sva.code==200){
            this.$router.replace({ path: "/operation/WithReducedActivity" });
          }
        })
      }
    },

    //重置
    resetForm() {
      this.ruleForm.discountId=null;
      this.ruleForm.title=null;
      this.ruleForm.activityState=null;
      this.ruleForm.startTime=null;
      this.ruleForm.endTime=null;
      this.ruleForm.putType=null;
      this.ruleForm.category=null;
      this.ruleForm.DiscountData.forEach(ref=>{
        ref.threshold=null
        ref.mode=null
      });
      this.$nextTick(()=>{
        this.$refs.tree.setCheckedKeys([]);
      })
      this.selectClass=null;
      var goodn = this.allGoods.goodsData;
      goodn.forEach((itemk, a) => {
        if (itemk.status == 1) {
          itemk.status = 0;
          this.selectGood.goodsDataA.splice(itemk, 1);
        }
      });
      var branda = this.allBank.brandData;
      branda.forEach((inda, i) => {
        if (inda.state == 1) {
          inda.state = 0;
          this.selectBank.brandDataA.splice(inda, 1);
        }
      });
    },

    //优惠券删除
    deleteOne(index, row) {
      var imda = this.ruleForm.DiscountData;
      var idms=this.idlistData
      imda.forEach((inem, i) => {
        if (index == i) {
          this.ruleForm.DiscountData.splice(i, 1);
          var mas={
            id:inem.id
          }
          idms.push(mas)
        }
      });
    },

    //返回
    Return() {
      this.$router.replace({ path: "/operation/WithReducedActivity" });
    },

    //添加优惠券
    addColumn() {
      var lev = this.ruleForm.DiscountData.length + 1;
      var dis = this.ruleForm.DiscountData;
      if (lev != 6) {
        var list = {
          level: lev,
          mode: this.mode,
          rebateId: this.ids,
          threshold: this.threshold
        };
        this.ruleForm.DiscountData.push(list);
      } else {
        alert("不能新增超过5个");
      }
    },

    //全部商品筛选查询
    Search() {
      this.currentPage4 = 1;
      this.pageSize = 8;
      this.indataAA(
        this.currentPage4,
        this.pageSize,
        this.allGoods.sortId,
        this.allGoods.brandId,
        this.allGoods.title
      );
    },

    //已选商品筛选查询
    SearchA() {
      this.currentPage4 = 1;
      this.pageSize = 8;
      this.indataBA(
        this.currentPage4,
        this.pageSize,
        this.allGoods.sortId,
        this.allGoods.brandId,
        this.allGoods.title
      );
    },

    //全部品牌筛选查询
    SearchB() {
      this.currentPage4 = 1;
      this.pageSize = 8;
      this.indataCA(this.currentPage4, this.pageSize, this.allBank.sortId);
    },

    //已选品牌筛选查询
    SearchC() {
      this.currentPage4 = 1;
      this.pageSize = 8;
      this.indataDA(this.currentPage4, this.pageSize, this.allBank.sortId);
      console.log(this.allBank.sortId);
    },

    // 全部商品分页
    handleSizeChange(val) {
      this.pageSize = val;
      console.log(`每页 ${val} 条`);
      if (this.selectrue == true) {
        this.indataAA(
          this.currentPage4,
          this.pageSize,
          this.allGoods.sortId,
          this.allGoods.brandId,
          this.allGoods.title
        );
      } else {
        this.indataA(this.currentPage4, this.pageSize, this.ids);
      }
    },
    handleCurrentChange(val) {
      this.currentPage4 = val;
      console.log(`当前页: ${val}`);
      if (this.selectrue == true) {
        this.indataAA(
          this.currentPage4,
          this.pageSize,
          this.allGoods.sortId,
          this.allGoods.brandId,
          this.allGoods.title
        );
      } else {
        this.indataA(this.currentPage4, this.pageSize, this.ids);
      }
    },

    //已选商品分页
    handleSizeChange1(val) {
      this.pageSize = val;
      console.log(`每页 ${val} 条`);
      if (this.selectrue == true) {
        this.indataBA(
          this.currentPage4,
          this.pageSize,
          this.selectGood.sortId,
          this.selectGood.brandId,
          this.selectGood.title
        );
      } else {
        this.indataB(this.currentPage4, this.pageSize, this.ids);
      }
    },
    handleCurrentChange1(val) {
      this.currentPage4 = val;
      console.log(`当前页: ${val}`);
      if (this.selectrue == true) {
        this.indataBA(
          this.currentPage4,
          this.pageSize,
          this.selectGood.sortId,
          this.selectGood.brandId,
          this.selectGood.title
        );
      } else {
        this.indataB(this.currentPage4, this.pageSize, this.ids);
      }
    },

    //全部品牌分页
    handleSizeChange2(val) {
      this.pageSize = val;
      console.log(`每页 ${val} 条`);
      if (this.selectrue == true) {
        this.indataCA(this.currentPage4, this.pageSize, this.allBank.sortId);
      } else {
        this.indataC(this.currentPage4, this.pageSize, this.ids);
      }
    },
    handleCurrentChange2(val) {
      this.currentPage4 = val;
      console.log(`当前页: ${val}`);
      if (this.selectrue == true) {
        this.indataCA(this.currentPage4, this.pageSize, this.allBank.sortId);
      } else {
        this.indataC(this.currentPage4, this.pageSize, this.ids);
      }
    },

    //已选品牌分页
    handleSizeChange3(val) {
      this.pageSize = val;
      console.log(`每页 ${val} 条`);
      if (this.selectrue == true) {
        this.indataDA(this.currentPage4, this.pageSize, this.selectBank.sortId);
      } else {
        this.indataD(this.currentPage4, this.pageSize, this.ids);
      }
    },
    handleCurrentChange3(val) {
      this.currentPage4 = val;
      console.log(`当前页: ${val}`);
      if (this.selectrue == true) {
        this.indataDA(this.currentPage4, this.pageSize, this.selectBank.sortId);
      } else {
        this.indataD(this.currentPage4, this.pageSize, this.ids);
      }
    },

    //全部商品全选按钮
    pageSelect() {
      var goods = this.allGoods.goodsData;
      goods.forEach((itemx, a) => {
        if (itemx.status == 0) {
          itemx.status = 1;
          var reobj = {};
          reobj.id = itemx.id;
          reobj.picture = itemx.picture;
          reobj.title = itemx.title;
          reobj.price = itemx.price;
          reobj.rebateId = this.ids;
          reobj.sortId = itemx.sortId;
          reobj.brandId = itemx.brandId;
          reobj.status = 1;
          this.selecData.push(reobj);
          this.selectGood.goodsDataA.push(reobj);
        }
      });
    },

    //全部商品全选取消按钮
    pageQuXiao() {
      var goodn = this.allGoods.goodsData;
      goodn.forEach((itemk, a) => {
        if (itemk.status == 1) {
          itemk.status = 0;
          this.selectGood.goodsDataA.splice(itemk, 1);
        }
      });
    },

    //已选商品本页面全选取消按钮
    pageQuXiaoA() {
      var goodl = this.allGoods.goodsData;
      goodl.forEach((index, i) => {
        if (index.status == 1) {
          index.status = 0;
          this.selectGood.goodsDataA.splice(index, 1);
        }
      });
    },

    //全部品牌本页面全选按钮
    pageSelectB() {
      var brands = this.allBank.brandData;
      brands.forEach((indx, i) => {
        if (indx.state == 0) {
          indx.state = 1;
          var braobj = {};
          braobj.id = indx.id;
          braobj.picture = indx.picture;
          braobj.name = indx.name;
          braobj.rebateId = this.ids;
          braobj.sortId = indx.sortId;
          braobj.state = 1;
          this.selectBank.brandDataA.push(braobj);
        }
      });
    },

    //全部品牌本页面全选取消按钮
    pageQuXiaoB() {
      var branda = this.allBank.brandData;
      branda.forEach((inda, i) => {
        if (inda.state == 1) {
          inda.state = 0;
          this.selectBank.brandDataA.splice(inda, 1);
        }
      });
    },

    //已选品牌本页面全选取消按钮
    pageQuXiaoC() {
      var brandu = this.allBank.brandData;
      brandu.forEach((inds, i) => {
        if (inds.state == 1) {
          inds.state = 0;
          this.selectBank.brandDataA.splice(inds, 1);
        }
      });
    },

    //点击未选择按键后push到已选商品
    SelectInt: function(item, row) {
      if (row.status == 0) {
        row.status = 1;
        var restobj = {};
        restobj.id = item;
        restobj.picture = row.picture;
        restobj.title = row.title;
        restobj.price = row.price;
        restobj.rebateId = this.ids;
        restobj.sortId = row.sortId;
        restobj.brandId = row.brandId;
        restobj.status = 1;
        this.selectGood.goodsDataA.push(restobj);
      } else if (row.status == 1) {
        row.status = 0;
        var ama = this.selectGood.goodsDataA;
        ama.forEach((item, i) => {
          if (row.id == item.id) {
            this.selectGood.goodsDataA.splice(i, 1);
          }
        });
      }
    },

    //在已选商品点击已选择按键后splice本页数据
    SelectIntA: function(items, rowa) {
      if (rowa.status == 1) {
        var kak = this.allGoods.goodsData;
        var mas = this.selectGood.goodsDataA;
        mas.forEach((itemd, a) => {
          if (items == itemd.id) {
            this.selectGood.goodsDataA.splice(a, 1);
          }
        });
        kak.forEach((item, i) => {
          if (rowa.id == item.id) {
            item.status = 0;
          }
        });
      }
    },

    //点击未选择按键后push到已选品牌
    SelectBrandA: function(itma, rowm) {
      if (rowm.state == 0) {
        rowm.state = 1;
        var list = {};
        list.id = itma;
        list.picture = rowm.picture;
        list.name = rowm.name;
        list.rebateId = this.ids;
        list.sortId = rowm.sortId;
        list.state = 1;
        this.selectBank.brandDataA.push(list);
      } else if (rowm.state == 1) {
        rowm.state = 0;
        var aam = this.selectBank.brandDataA;
        aam.forEach((itee, c) => {
          if (rowm.id == itee.id) {
            this.selectBank.brandDataA.splice(c, 1);
          }
        });
      }
    },

    //在已选品牌点击已选择按键后splice本页数据
    SelectBrandB: function(itmm, rowma) {
      if (rowma.state == 1) {
        var kam = this.allBank.brandData;
        var kkm = this.selectBank.brandDataA;
        kkm.forEach((itaam, d) => {
          if (itmm == itaam.id) {
            this.selectBank.brandDataA.splice(d, 1);
          }
        });
        kam.forEach((itao, e) => {
          if (rowma.id == itao.id) {
            itao.state = 0;
          }
        });
      }
    },

    //选项卡跳转选择监听
    agreeChange: function(val) {
      console.log(val);
      this.activeGood = val;
    }
  }
};
</script>
<style scoped>
.rightTop {
  float: right;
}
.mainList {
  float: left;
}
.addColumn {
  background-color: orange;
  color: aliceblue;
}
</style>
